package com.hwbsite.hwbback.config;

import lombok.AllArgsConstructor;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@EnableCaching
@Configuration
@AllArgsConstructor
public class RedisConfig  extends CachingConfigurerSupport {

    private final RedisProperties properties;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        return redisTemplate((0));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate1() {
        return redisTemplate((1));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate2() {
        return redisTemplate((2));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate3() {
        return redisTemplate((3));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate4() {
        return redisTemplate((4));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate5() {
        return redisTemplate((5));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate6() {
        return redisTemplate((6));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate7() {
        return redisTemplate((7));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate8() {
        return redisTemplate((8));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate9() {
        return redisTemplate((9));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate10() {
        return redisTemplate((10));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate11() {
        return redisTemplate((11));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate12() {
        return redisTemplate((12));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate13() {
        return redisTemplate((13));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate14() {
        return redisTemplate((14));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate15( ) {
        return redisTemplate(15);
    }


    private  RedisTemplate<String, Object> redisTemplate(int DataBase) {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(properties.getHost());
        redisStandaloneConfiguration.setDatabase(DataBase);
        redisStandaloneConfiguration.setPassword(properties.getPassword());

        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration,
                LettuceClientConfiguration.builder().build());
        lettuceConnectionFactory.afterPropertiesSet();

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);

        return redisTemplate;
    }

}
